// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

int t, n;

const int N = 3e5+50;

struct Bit {
    int dat[N];
    inline int lowbit(int x) {
        return x & (-x);
    }
    inline void add(int i, int x) {
        for (; i<N; i+=lowbit(i))
            dat[i] += x;
    }
    inline int sum(int i) {
        int res = 0;
        for (; i; i-=lowbit(i))
            res += dat[i];
        return res;
    }
} bit[2];


struct segT
{
    LL dat[N << 2];
 
    LL lazy[N << 2];
 
    int nn, L, R;
 
    inline void pu(int rt)
    {
        dat[rt] = max(dat[rt << 1], dat[rt << 1 | 1]);
    }
 
 
    inline void pd(int rt)
    {
        if (lazy[rt])
        {
            int ls = rt << 1, rs = rt << 1 | 1;
            lazy[ls] += lazy[rt];
            dat[ls] += lazy[rt];
            lazy[rs] += lazy[rt];
            dat[rs] += lazy[rt];
            lazy[rt] = 0;
        }
    }
    void init(int n)
    {
        nn = 1;
        while (nn < n) nn <<= 1;
        // for (int i=1; i<=n; ++i)
        //     dat[i+nn-1] = node(i, a[i]);
        // for (int i=nn + n; i<2*nn; ++i)
        //     dat[i] = node(-1, -1e18);
        // for (int i=nn-1; i>=1; --i)
        //     pu(i);
    }
 
    void u(int l, int r, int rt, int v)
    {
        // dbg(l, r, rt, v);
        if (L <= l && r <= R)
        {
            dat[rt] += v;
            lazy[rt] += v;
            return;
        }
        pd(rt);
        int m = (l + r) >> 1;
        if (L <= m) u(l, m, rt << 1, v);
        if (m+1<=R) u(m+1, r, rt << 1 | 1, v);
        pu(rt);
    }
 
    LL q(int l, int r, int rt)
    {
        // dbg(l, r, rt);
        if (L <= l && r <= R)
        {
            return dat[rt];
        }
        int m = (l+r) >> 1;
        pd(rt);
        LL v1 = 0, v2 = 0;
        if (L <= m) v1 = q(l, m, rt<<1);
        if (m+1<=R) v2 = q(m+1, r, rt<<1|1);
        pu(rt);
        return max(v1, v2);
    }
 
    inline void u(int l, int r, int v)
    {
        ++l; ++r;
        if (l > r) return;
        dbg("FUCK", l, r);
        L = l;
        R = r;
        u(1, nn, 1, v);
    }
 
    inline LL q(int l, int r)
    {
        ++l; ++r;
        L = l;
        R = r;
        return q(1, nn, 1);
    }
 
} seg[2];

pair<int, int> dat[N], a[N << 1];
int dcnt, acnt;

char s[N];

int RL[N << 1], b[N << 1], bcnt;
void manacher(int* a, int n) { // "abc" => "#a#b#a#"
    int r = 0, p = 0;
    for (int i=0; i<n; ++i) {
        if (i < r) RL[i] = min(RL[2 * p - i], r - i);
        else RL[i] = 1;
        while (i - RL[i] >= 0 && i + RL[i] < n && a[i - RL[i]] == a[i + RL[i]])
            RL[i]++;
        if (RL[i] + i - 1 > r) { r = RL[i] + i - 1; p = i; }
    }
    for (int i=0; i<n; ++i) --RL[i];
}

LL sum(int n) {
    return 1LL * n * (n + 1) / 2;
}

LL sum(int l, int r) {
    if (l > r) return 0;
    if (l == 0) return sum(r);
    else return sum(r) - sum(l-1);
}


int main(int argc, char const *argv[]) {
    // code
    scanf("%d%s", &n, s);
    for (int i=0, j; i<n; i=j+1) {
        j = i;
        while (j+1<n && s[j+1] == s[i])
            ++j;
        dat[dcnt++] = make_pair(i, j);
    }
    LL ans = 0;
    for (int i=0; i<dcnt; ++i) {
        if (i == dcnt - 2) {
            ans += sum(n - dat[i].second, n - dat[i].first - 1);
            ans -= dat[i].second - dat[i].first;
            dbg(ans);
            continue;
        }
        if (dat[i].first == dat[i].second && i < dcnt - 2) {
            ans += n - dat[i+2].first;
            continue;
        }
        ans += sum(n - dat[i].second, n - dat[i].first - 1);
        if (i < dcnt-2)
            ans += n - dat[i+2].first;
        if (i < dcnt-1) {
            ans -= dat[i].second - dat[i].first;
        }
        dbg(n - dat[i].second - 1, n - dat[i].first - 1);
        dbg(i, dat[i].second - dat[i].first + 1);
        dbg(ans);
    }
    cout << ans << endl;


    return 0;
}
